package com.woniuxy.enjoylife.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.enjoylife.entity.House;
import com.woniuxy.enjoylife.entity.Server;
import com.woniuxy.enjoylife.entity.User;
import com.woniuxy.enjoylife.entity.dto.ReturnServerDto;
import com.woniuxy.enjoylife.entity.dto.SelectServerDto;
import com.woniuxy.enjoylife.mapper.CommunityMapper;
import com.woniuxy.enjoylife.mapper.HouseMapper;
import com.woniuxy.enjoylife.mapper.ServerMapper;
import com.woniuxy.enjoylife.mapper.UserMapper;
import com.woniuxy.enjoylife.service.CommunityService;
import com.woniuxy.enjoylife.service.ServerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.enjoylife.utils.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author teamEnjoyLife75
 * @since 2021-01-19
 */
@Service
public class ServerServiceImpl extends ServiceImpl<ServerMapper, Server> implements ServerService {

    @Resource
    private HouseMapper houseMapper;
    @Resource
    private ServerMapper serverMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommunityMapper communityMapper;

    @Override
    public PageResult<ReturnServerDto> selectServer(SelectServerDto ssd) {

        //设置小区id，服务类型
        Server server = new Server().setCid(ssd.getCid()).setType(ssd.getType());
        //设置模糊查询
        if (null != ssd.getName() && !ssd.getName().equals("")) {
            server.setName("%" + ssd.getName() + "%");
        }

        //分页查询服务
        Page<Server> pageHelper = PageHelper.startPage(ssd.getCurrentPage(), ssd.getSize());
        List<ReturnServerDto> serverList = serverMapper.select(server);

        //服务名模糊查询失败时，使用内容模糊查询
        if (serverList.size() == 0) {
            if (null != ssd.getContent() && !ssd.getContent().equals("")) {
                server.setName(null);
                server.setContent("%" + ssd.getContent() + "%");
            }
            pageHelper = PageHelper.startPage(ssd.getCurrentPage(), ssd.getSize());
            serverList = serverMapper.select(server);
        }

        PageResult<ReturnServerDto> pageResult = new PageResult<>();

        pageResult.setCurrentPage(ssd.getCurrentPage());
        pageResult.setPageSize(ssd.getSize());
        pageResult.setTotal(pageHelper.getTotal());
        pageResult.setTotalPage(pageHelper.getPages());
        pageResult.setData(serverList);
        return pageResult;
    }


    @Override
    public PageResult<ReturnServerDto> userSelectServer(SelectServerDto ssd) {
        //获取小区id
        House house = houseMapper.selectHouse(ssd.getUid());
        ssd.setCid(house.getCid());
        ssd.setUid(null);
        return this.selectServer(ssd);
    }

    @Override
    public List<Server> sorting(String sortCol, String sort) {

        List<Server> servers = serverMapper.sorting(sortCol, sort);

        return servers;
    }


}
